import 'package:flutter_admin/api/odoo/mrp_workorder_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/mrp_production.dart';
import 'package:flutter_admin/models/odoo/mrp_workorder.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input_num.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:quiver/strings.dart';

class MrpWorkorderList extends StatefulWidget {
  MrpProduction? mrpProduction;
  MrpWorkorderList({required this.mrpProduction, Key? key}) : super(key: key);

  @override
  _MrpWorkorderListState createState() => _MrpWorkorderListState();
}

class _MrpWorkorderListState extends State<MrpWorkorderList> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  late MrpProduction mrpProduction;
  MrpWorkorder mrpWorkorder = MrpWorkorder();
  List tableList = [];
  @override
  void initState() {
    super.initState();
    this.mrpProduction = widget.mrpProduction ?? MrpProduction();
    mrpWorkorder.mrpProductionId = mrpProduction.id;
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _loadData();
    });
  }

  stateMap(e) {
    return e == 'pending'
        ? '等待其他工单'
        : e == 'ready'
            ? '就绪'
            : e == 'progress'
                ? '进行中'
                : e == 'done'
                    ? '已完工'
                    : e == 'cancel'
                        ? '已取消'
                        : '--';
  }
  stateMapMain(e) {
    return e == 'confirmed'
        ? '已确认'
        : e == 'progress'
        ? '进行中'
        : e == 'to_close'
        ? '待关闭'
        : e == 'done'
        ? '完成'
        : e == 'cancel'
        ? '已取消'
        : '--';
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            width: 300,
            label: '生产单号',
            enable: false,
            value: mrpProduction.name,
          ),
          CryInput(
            width: 300,
            label: '产品图号',
            enable: false,
            value: mrpProduction.productCode,
          ),
          CryInput(
            width: 300,
            label: '产品名称',
            enable: false,
            value: mrpProduction.productName,
          ),
          CryInput(
            width: 300,
            label: '投产开始时间',
            enable: false,
            value: mrpProduction.dateStart,
          ),
          CryInput(
            width: 300,
            label: '投产结束时间',
            enable: false,
            value: mrpProduction.dateFinished,
          ),
          CryInputNum(
            width: 300,
            label: '数量',
            enable: false,
            value: mrpProduction.productQty,
          ),
          CryInput(
            width: 300,
            label: '单位',
            enable: false,
            value: mrpProduction.productUom,
          ),
          CryInput(
            width: 300,
            label: '状态',
            enable: false,
            value: stateMapMain(mrpProduction.state),
          ),
        ],
      ),
    );
    var table = DataTable(
      columns: [
        DataColumn(label: Text('工序')),
        DataColumn(label: Text("车间")),
        DataColumn(label: Text("计划开始时间")),
        DataColumn(label: Text("状态")),
      ],
      rows: this.tableList.map((m) {
        MrpWorkorder mrpWorkorder = MrpWorkorder.fromMap(m);

        var result = DataRow(cells: [
          DataCell(Text(mrpWorkorder.userName ?? '--')),
          DataCell(Text(mrpWorkorder.locationName ?? '--')),
          DataCell(Text(mrpWorkorder.dateStart ?? '--')),
          // DataCell(Text(isBlank(mrpWorkorder.dateStart)
          //     ? '--'
          //     : Utils.timeUTC(mrpWorkorder.dateStart))),
          DataCell(Text(stateMap(mrpWorkorder.state))),
        ]);
        return result;
      }).toList(),
    );

    var result = Scaffold(
      key: scaffoldStateKey,
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
            final bool canPop = parentRoute?.canPop ?? false;

            if (canPop) {
              return BackButton();
            } else {
              return SizedBox.shrink();
            }
          },
        ),
        title: Text('订单明细'),
      ),
      body: SingleChildScrollView(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          form,
          SingleChildScrollView(scrollDirection: Axis.horizontal, child: table),
        ],
      )),
    );
    return result;
  }

  _loadData() async {
    ResponseBodyApi responseBodyApi = await MrpWorkorderApi.page(data: {
      "domainList": mrpWorkorder.toDomainList(),
    });
    if (responseBodyApi.success!) {
      tableList = responseBodyApi.data;
    }
    setState(() {});
  }
}
